package bigbrother;

import bigbrother.core.IrcBotCore;
import bigbrother.modules.help.HelpModule;
import bigbrother.modules.logging.LogModule;
import bigbrother.modules.time.TimeModule;

import java.util.logging.Logger;

/**
 * BigBrother IRC Bot, main entry point. Handles command line arguments, and setting up modules.
 * <p/>
 * If you want to use the BigBrother IRC Bot as a library instead, use {@link IrcBotCore} directly.
 *
 * @author Hans Haggstrom
 */
public final class BigBrother
{

    //======================================================================
    // Private Fields

    private final IrcBotCore myIrcBot;

    //======================================================================
    // Private Constants

    private static final String BOT_NAME = "BigBrother";
    private static final String VERSION = "0.0.1 SNAPSHOT";  // TODO: How to get the version string from the maven pom.xml file set here automatically?
    private static final String DEFAULT_CONFIG_LOCATION = "ircbot.conf";
    private static final Logger LOGGER = Logger.getLogger( BigBrother.class.getName() );

    //======================================================================
    // Public Methods

    //----------------------------------------------------------------------
    // Main Method

    /**
     * Starts up the BigBrother IRC bot, reads the configuration file, and connects to the specified server.
     *
     * @param args contains command line arguments.  Currently takes the config file as the first argument. If no
     *             arguments are given, it assumes the config file is in the default location (current folder, named
     *             ircbot.config).
     */
    public static void main( String[] args )
    {
        final String configFileLocation = getConfigFileName( args, DEFAULT_CONFIG_LOCATION );

        new BigBrother( configFileLocation );
    }

    //----------------------------------------------------------------------
    // Constructors

    /**
     * Creates a new BigBrother IRC Bot, and connects it to the server specified in the default config file.
     */
    public BigBrother()
    {
        this( null );
    }


    /**
     * Creates a new BigBrother IRC Bot, and connects it to the server specified in the specified config file.
     *
     * @param configFileLocation location of the config file to use.  If null, the default location is used.
     */
    public BigBrother( String configFileLocation )
    {
        LOGGER.info( "Starting up " + BOT_NAME + " Irc Bot." );

        if ( configFileLocation == null )
        {
            configFileLocation = DEFAULT_CONFIG_LOCATION;
        }

        myIrcBot = new IrcBotCore( BOT_NAME, VERSION, configFileLocation );

        // Register available default modules.
        myIrcBot.addIrcModule( new TimeModule() );
        myIrcBot.addIrcModule( new LogModule() );
        myIrcBot.addIrcModule( new HelpModule() );

        myIrcBot.startIrcBot();
    }

    //----------------------------------------------------------------------
    // Other Public Methods

    public IrcBotCore getIrcContext()
    {
        return myIrcBot;
    }

    //======================================================================
    // Private Methods

    private static String getConfigFileName( final String[] args, final String defaultConfigLocation )
    {
        String configFile = defaultConfigLocation;
        if ( args.length > 0 )
        {
            configFile = args[ 0 ];
        }

        return configFile;
    }

}
